﻿using Base.FileRemoteModel.Down;
using Base.FileRemoteModel.Down.Model;
using Base.FileRemoteModel.UpFile;
using Base.FileRemoteModel.UpFile.Model;
using Basic.WxExtendService.Interface;
using WeDonekRpc.Client;
using WeDonekRpc.Client.Attr;
using WeDonekRpc.Helper;

namespace Basic.WxExtendService.Extend
{
    [ClassLifetimeAttr(ClassLifetimeType.SingleInstance)]
    internal class FileApiService : IFileApiService
    {
        private static readonly string _DefUserType = RpcClient.SystemTypeVal;
        private static readonly HttpRequestArg _DefRequestArg = new HttpRequestArg
        {
            Referer = "https://mp.weixin.qq.com/"
        };

        public FileSaveResult SaveFileStream (FileSaveDatum datum, byte[] stream)
        {
            datum.UserType = _DefUserType;
            return new SaveFileStream
            {
                Datum = datum,
                Stream = stream
            }.Send();
        }
        public DownResult DownWxImage (Uri uri, long? linkBizPk, string dirKey, string fileName)
        {
            return new DownSmallFile
            {
                DownUri = uri,
                Param = new DownFileParam
                {
                    DirKey = dirKey,
                    FileName = fileName,
                    LinkBizPk = linkBizPk,
                    UserType = _DefUserType,
                    RequestSet = _DefRequestArg
                }
            }.Send();
        }
        public DownResult DownWxImage (Uri uri, string dirKey, string fileName)
        {
            return this.DownWxImage(uri, null, dirKey, fileName);
        }

        public void Drop (string[] uris)
        {
            long[] ids = uris.Where(c => !c.IsNull()).Select(c => long.Parse(Path.GetFileNameWithoutExtension(c))).ToArray();
            if (ids.Length > 0)
            {
                new BatchDropFileById
                {
                    Ids = ids
                }.Send();
            }
        }

        public void Save (long[] fileId, long id)
        {
            new SaveFileList
            {
                FileId = fileId,
                LinkBizPk = id
            }.Send();
        }
    }
}
